package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.BanJi;
import com.example.demo.entity.XueYuan;
import com.example.demo.service.BanJiService;
import com.example.demo.service.XueYuanService;
import com.example.demo.vo.BanJiVo;
import com.example.demo.vo.DataView;
import com.example.demo.vo.XueYuanVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import static com.example.demo.service.BanJiService.*;

/**
 * @author Coding路人王
 * @date 2022/6/10 8:31
 */
@RestController
public class BnJiController {
    @Autowired
    private BanJiService banJiService;


    @RequestMapping("/toBanJi")
    public String toXueYuan(){
        return "banji/banji";
    }



    @RequestMapping("/listBanJi")
    @ResponseBody
    public DataView listBanJi(BanJiVo banJiVo) {
        QueryWrapper<BanJi> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(banJiVo.getName()),"name",banJiVo.getName());
        IPage<BanJi> iPage = new Page<>(banJiVo.getPage(),banJiVo.getLimit());
        banJiService.page(iPage,queryWrapper);
        return new DataView(iPage.getTotal(),iPage.getRecords());
    }



    /**
     * 新增
     */
    @RequestMapping("/addBanJi")
    @ResponseBody
    public DataView addBanJi(BanJi banJi){
        boolean b = banJiService.saveOrUpdate(banJi);
        DataView dataView = new DataView();
        if (b){
            dataView.setCode(200);
            dataView.setMsg("添加成功！");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("添加失败！");
        return dataView;
    }

    @RequestMapping("/updateBanJi")
    @ResponseBody
    public DataView updateBanJi(BanJi banJi){
        banJiService.updateById(banJi);
        DataView dataView = new DataView();
        boolean b = banJiService.saveOrUpdate(banJi);
        dataView.setCode(200);
        dataView.setMsg("修改成功！");
        return dataView;
    }



    @RequestMapping("/deleteBanJi")
    @ResponseBody
    public DataView deleteBanJi(Integer id){
        banJiService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除成功！");
        return dataView;
    }
}
